# vim: ts=8 sw=4 syn=make
#
# Copyright 2013 (c) Garry T. Williams
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

INSTALL   = install
POD2MAN   = pod2man
SYSTEMCTL = /usr/bin/systemctl
PFLAGS    = -r backlight
CFLAGS    = -g -Wall
EXECS     = backlight
UNITS     = backlight.service
MANS      = $(EXECS:=.1)
BIN       = $(PREFIX)/bin
MAN       = $(PREFIX)/man
UNIT      = /etc/systemd/system

.SUFFIXES: .h .1

.h.1:
	@ echo generating manual page $@...
	@ $(POD2MAN) $(PFLAGS) -s1 -n$* $< >$@

all: $(EXECS) $(MANS) $(UNITS)

install: _have_prefix all $(BIN) $(MAN)
	@ for f in $(EXECS) ; do \
	    echo installing $$f in $(BIN) ; \
	    $(INSTALL) -m 0755 $$f $(BIN) ; \
	done
	@ for f in $(MANS) ; do \
	    n=`echo $$f | sed 's/.*\.//'` ; \
	    echo installing $$f in $(MAN)/man$$n ; \
	    mkdir -p $(MAN)/man$$n ; \
	    $(INSTALL) -m 0644 $$f $(MAN)/man$$n ; \
	done
	@ for f in $(UNITS) ; do \
	    echo installing $$f in $(UNIT) ; \
	    $(INSTALL) -m 0644 $$f $(UNIT) ; \
	    $(SYSTEMCTL) enable $$f ; \
	done

clean:
	rm -f *~ core.* $(EXECS) $(MANS) $(EXECS:=.o)

$(BIN):
	mkdir -p $(BIN)

$(MAN):
	mkdir -p $(MAN)

_have_prefix:
	@ if test "$(PREFIX)" = "" ; then \
	    echo you must set the PREFIX variable for this target ; \
	    exit 1 ; \
	fi

backlight.o: backlight.h

